package com.qianbao.controller.v1.book;



import com.qianbao.components.excepion.CommonException;
import com.qianbao.components.http.SVRequestConfig;
import com.qianbao.components.svresut.Result;
import com.qianbao.components.svversion.ApiVersion;
import com.qianbao.domain.book.BookCategory;
import com.qianbao.domain.book.BookEntity;
import com.qianbao.domain.book.BookListRequestEntity;
import com.qianbao.service.book.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;


@Controller
@ApiVersion(1)
@RequestMapping("/bookcenter/{version}/")
public class BookController {


    @Autowired
    private BookService bookService;

    /**
     * 获取所有的书籍分类
     * @return
     */
    @RequestMapping("/book-category")
    @ResponseBody
    public Result attachBookCategory(){

        try {
            Map<String, List<BookCategory>> listMap = bookService.requestBookCategory(
                    SVRequestConfig.getJeesite_url()
                            + "/jeesitestock/book/book_category");
            return Result.success(listMap);
        } catch (CommonException e) {
            e.printStackTrace();
            return Result.fail(e.getCode(),e.getMessage());
        }
    }


    /**
     * 根据分类id获取分类列表
     * @param entity
     * @return
     */

    @RequestMapping("/book-list")
    @ResponseBody
    public Result attachBookList(@Valid BookListRequestEntity entity){

        try {
            Map<String, List<BookEntity>> map = bookService.requestBookList(SVRequestConfig.getJeesite_url() + "/jeesitestock/book/category_list", entity);
            return Result.success(map);
        } catch (CommonException e) {
            e.printStackTrace();
            return Result.fail(e.getCode(),e.getMessage());
        }

    }








}
